package com.liam.customer.controller;

import com.liam.customer.model.Customer;
import com.liam.customer.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.smart4j.framework.annotation.Action;
import org.smart4j.framework.annotation.Aspect;
import org.smart4j.framework.annotation.Controller;
import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.bean.Data;
import org.smart4j.framework.bean.Param;
import org.smart4j.framework.bean.View;

import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/9/14.
 */
@Controller
public class CustomerController {
    @Inject
    private CustomerService customerService;



    @Action("get:/customer")
    public View index(Param param){
        List<Customer> customerList = customerService.getCustomerList("");
        return new View("customer/list.jsp").addModel("customerList",customerList);
    }

    @Action("get:/customer_edit")
    public View show(Param param){
        String id = param.getString("id");
        Customer customer = customerService.getCustomer(Long.valueOf(id));
        return new View("customer/create.jsp").addModel("customer",customer);
    }

    @Action("post:/customer_save")
    public Data save(Param param){
        String id = param.getString("id");
        Map<String,Object> fieldMap = param.getFieldMap();
        if(StringUtils.isNotEmpty(id)){
            boolean result = customerService.updateCustomer(Long.valueOf(id),fieldMap);
            return new Data(result);
        }else{
            boolean result = customerService.crateCustomer(fieldMap);
            return new Data(result);
        }
    }

    @Action("get:/customer_create")
    public View toCreate(Param param){
        return new View("customer/create.jsp");
    }
    @Action("post:/customer_create")
    public View create(Param param){
        Map<String,Object> fieldMap = param.getFieldMap();
        boolean result = customerService.crateCustomer(fieldMap);
        return new View("/customer");
    }
    @Action("get:/customer_delete")
    public View delete(Param param){
        String id = param.getString("id");
        boolean result = customerService.deleteCustomer(Long.valueOf(id));
        return new View("/customer");
    }

}
